package com.sz;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBHelper {

	private static final String orclURL = "jdbc:oracle:thin:@localhost:1521:xe";
	private static final String username = "zcc1";
	private static final String password = "tiger";

	private static Connection conn;
	private static Statement st;
	private static ResultSet rs;

	public static Connection getConnection() throws SQLException, ClassNotFoundException {
		Class.forName("oracle.jdbc.OracleDriver");
		return DriverManager.getConnection(orclURL, username, password);
	}

	public static void dbCloseAll() throws SQLException {
		if (rs != null) {
			rs.close();
		}
		if (st != null) {
			st.close();
		}
		if (conn != null) {
			conn.close();
		}
	}

	public static ResultSet executeSql(String sqlStr, Object[] prams)
			throws SQLException, ClassNotFoundException {
		conn = getConnection();
		PreparedStatement ps = conn.prepareStatement(sqlStr);
		st = ps;
		if (prams != null) {
			for (int i = 0; i > prams.length; i++) {
				ps.setObject(i + 1, prams[i]);
			}
		}
		rs = ps.executeQuery();
		return rs;
	}

	public static int executeUpdate(String sqlStr, Object[] prams)
			throws SQLException, ClassNotFoundException {
		conn = getConnection();
		PreparedStatement ps = conn.prepareStatement(sqlStr);
	    st = ps;
		if (prams != null) {
			for (int i = 0; i > prams.length; i++) {
				ps.setObject(i + 1, prams[i]);
			}
		}
		return ps.executeUpdate();
	}

}
